package com.dongdongshop.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.CartService;
import com.dongdongshop.util.CookieUtils;
import com.dongdongshop.vo.CartVO;
import em.ResuleEnum;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("cart")
public class CartController {

    @DubboReference(timeout = 100000000)
    private CartService cartService;


    //添加购物车未登录
    @RequestMapping("addCart")
    public String addCart (HttpServletRequest request, HttpServletResponse response, Long skuId, Integer num){

        List<CartVO> cartList = getCartList(request,response);
        // 3.调用购物车服务,去帮我们追加购物车数据
        cartList = cartService.addCart(cartList,skuId,num);
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (tbUser != null){
            cartService.addCartListToRedis(cartList,tbUser.getId());
        }else {
            //4.保存到cookie
            CookieUtils.setCookie(request, response, "CartList", JSONObject.toJSONString(cartList), true);
        }
        return "success-cart";
    }

    //没登陆查询购物车数据
    @RequestMapping("getCartList")
    @ResponseBody
    public List<CartVO> getCartList (HttpServletRequest request,HttpServletResponse response){
        //1、我们添加购物车前 要先获取之前的购物车数据  ,如果添加之前有数据就进行追加 如果没有就创建
        //cookie获取原先的数据
        String cartList = CookieUtils.getCookieValue(request, "CartList",true);
        //如果取不出来就我们就要创建
        if (StrUtil.isBlank(cartList)){
            cartList = "[]";
        }
        //2、将我们取出的cookie中得值,转成List
        List<CartVO> cartVOS = JSONObject.parseArray(cartList, CartVO.class);

        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (tbUser != null){
            List<CartVO> cartListFromRedis = cartService.selectCartListFromRedis(tbUser.getId());
            if (cartVOS.size() > 0){
                //把cookie的购物车与redis中的购物车合并 ,然后删除cookie的购物车,
                List<CartVO> mergeCartList = cartService.mergeCartList(cartVOS,cartListFromRedis);
                CookieUtils.deleteCookie(request,response,"CartList");
                //并将合并后的购物车数据重新放回到redis 并且返回合并后的数据
                cartService.addCartListToRedis(mergeCartList,tbUser.getId());
                return mergeCartList;
            }
            return cartListFromRedis;
        }else{
            return cartVOS;
        }
    }


    @RequestMapping("addCartAjax")
    @ResponseBody
    public AjaxResult addCartAjax(HttpServletRequest request,HttpServletResponse response,Long skuId,Integer num) {
        List<CartVO> cartList = getCartList(request, response);
        // 3.调用购物车服务,去帮我们追加购物车数据
        cartList = cartService.addCart(cartList, skuId, num);
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (tbUser != null) {
            cartService.addCartListToRedis(cartList, tbUser.getId());
        } else {
            //4.保存到cookie
            CookieUtils.setCookie(request, response, "CartList", JSONObject.toJSONString(cartList), true);
        }
        return AjaxResult.R(ResuleEnum.SUCCESS).setData(cartList);
    }
}
